#include <Python.h>
#include <iostream>
#include <chrono>
#include <thread>

using namespace std;

void py1(){
	Py_SetPythonHome(L"/home/st/ubuntu_data/software/miniconda3/envs/solov2"); //virtual enviroment path setting, otherwise don't work
	Py_Initialize(); //使用python前要调用此函数，进行初始化,开启虚拟机
	if( !Py_IsInitialized()){
		cout << "python init fail" << endl;
		return;
	}
	cout<<"ok1"<<endl;
	PyRun_SimpleString("import sys");
	PyRun_SimpleString("sys.path.append('/home/st/ubuntu_data/NRSL/little_project/cpp_learning/parallel_test/script')");

	PyObject* pModule = PyImport_ImportModule("parall_test");
	if( pModule == NULL ){
		cout <<"module not found" << endl;
		return;
	}
	cout<<"ok1"<<endl;
 	PyObject* pFunc1 = PyObject_GetAttrString(pModule, "func1");
    if( !pFunc1 || !PyCallable_Check(pFunc1)){
        cout <<"not found function add_num" << endl;
        return;
    }
	cout<<"ok1"<<endl;
    PyObject_CallObject(pFunc1, NULL );
    Py_Finalize();
}

void py2(){
	Py_SetPythonHome(L"/home/st/ubuntu_data/software/miniconda3/envs/ciassd_tra_pre"); //virtual enviroment path setting, otherwise don't work
	Py_Initialize(); //使用python前要调用此函数，进行初始化,开启虚拟机
	if( !Py_IsInitialized()){
		cout << "python init fail" << endl;
		return;
	}

	PyRun_SimpleString("import sys");
	PyRun_SimpleString("sys.path.append('/home/st/ubuntu_data/NRSL/little_project/cpp_learning/parallel_test/script')");

	PyObject* pModule = PyImport_ImportModule("parall_test");
	if( pModule == NULL ){
		cout <<"module not found" << endl;
		return;
	}

 	PyObject* pFunc2 = PyObject_GetAttrString(pModule, "func2");
    if( !pFunc2 || !PyCallable_Check(pFunc2)){
        cout <<"not found function add_num" << endl;
        return;
    }
	cout<<"ok2"<<endl;
    PyObject_CallObject(pFunc2, NULL );
    Py_Finalize();
}

int main(){

	std::chrono::system_clock::time_point t1, t2;
	t1 = std::chrono::system_clock::now();
	// py1();
	// py2();
	// thread th1(py1);
	// thread th2(py2);
	// th1.join();
	// th2.join();
	t2 = std::chrono::system_clock::now();
	cout<<std::chrono::duration_cast<std::chrono::microseconds>( t2-t1 ).count()<<std::endl;

	return 0;
}
